﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Simulacion.NumberGenerator;

namespace Simulacion.Distributions
{
    /// <summary>
    ///  Generic class of the Cauchy distribution.
    /// </summary>
    class Cauchy
    {
        public static double fCauchy(double x, double mi, double sigma)
        {
            return (Math.Pow((Math.PI * sigma * (1 + Math.Pow(((x - mi) / sigma), 2.0))), -1.0));
        }

        /// <summary>
        /// Gets the value using the Cauchy distribution.
        /// </summary>
        /// <returns>The value of the distribution.</returns>
        public static double GetDistributionValue(double lowerBound, double upperBound, double mi, double sigma)
        {
            double _totalArea, _boundedArea, _lowerBoundArea;
            double R = Generator.NextNumber;

            _totalArea = 180.0 / Math.PI;
            _boundedArea = (Math.Atan((upperBound - mi) / sigma) / Math.PI - Math.Atan((lowerBound - mi) / sigma) / Math.PI) * 180.0 / Math.PI;
            _lowerBoundArea = (Math.Atan((lowerBound - mi) / sigma) / Math.PI) * 180.0 / Math.PI + 90.0 / Math.PI;

            R = (_lowerBoundArea / _totalArea) + R * (_boundedArea / _totalArea);

            return (sigma * Math.Tan(Math.PI * (R - 0.5)) + mi) / 7;
        }
    }
}
